/**
 * 
 */
package com.xbang.attendance.web.action.purchase.purchaselist;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.order.PurchaseDetails;
import com.xbang.attendance.entity.order.PurchaseOrder;
import com.xbang.attendance.entity.system.Region;
import com.xbang.attendance.service.PurchaseOrderService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

import enumeration.DeliveryStatus;

/**
 * @author dev.zhang
 * 
 */
public class Search extends SearchActionSupport {

	private static final Log logger = LogFactory.getLog(Search.class);

	private static final long serialVersionUID = -1120551442020217513L;

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	private List<PurchaseOrder> purchaseOrders;

	private List<Region> regions = new ArrayList<Region>();

	private List<PurchaseDetails> purchaseDetails = new ArrayList<PurchaseDetails>();

	private Integer id;

	private PurchaseOrder purchaseOrder;

	public String execute() throws Exception {

		regions = purchaseOrderService.getObjects(Region.class);

		return "search";
	}

	/**
	 * @return
	 * @throws Exception
	 */
	@Action("/purchase/purchaselist/search/do")
	public String doSearch() throws Exception {
		
		logger.info("call search method by product ");
		String hqlSentence = "FROM PurchaseOrder purchaseorder WHERE 1 = 1 ";
		if (searchComponent.getOrderParameters().isEmpty()) {
			searchComponent.addSearchElement("orderBy.purchaseorder.orderDate", "DESC");
		}
		Result result = purchaseOrderService.searchByPage(hqlSentence, searchComponent, page);
		purchaseOrders = result.getItems();
		page = result.getPage();

		return "index";
	}

	@Action("/purchase/purchasedetails/${id}/search")
	public String detailSearch() throws Exception {
		regions = purchaseOrderService.getObjects(Region.class);

		return "detail-search";
	}

	@Action("/purchase/purchasedetails/${id}/search/do")
	public String doDetailSearch() throws Exception {
		if (id != null) {
			purchaseOrder = purchaseOrderService.getPurchaseOrderByKey(id);
			String hqlSentence = " FROM PurchaseDetails purchasedetails WHERE 1 = 1 ";
			searchComponent.addSearchElement("eq.purchasedetails.purchaseOrder.id", id);
			Result result = purchaseOrderService.searchByPage(hqlSentence, searchComponent, page);

			purchaseDetails = result.getItems();
			page = result.getPage();
		}
		return "detail";
	}

	@Action("/purchase/purchasedetails/${id}")
	public String details() throws Exception {

		if (id != null) {
			purchaseOrder = purchaseOrderService.getPurchaseOrderByKey(id);
			String hqlSentence = "FROM PurchaseDetails purchasedetails WHERE 1 = 1";
			searchComponent.addSearchElement("eq.purchasedetails.purchaseOrder.id", id);
			Result result = purchaseOrderService.searchByPage(hqlSentence, searchComponent, page);
			purchaseDetails = result.getItems();
			page = result.getPage();
		}

		return "detail";
	}

	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub

	}

	/**
	 * @return the purchaseOrders
	 */
	public List<PurchaseOrder> getPurchaseOrders() {
		return purchaseOrders;
	}

	/**
	 * @param purchaseOrders
	 *            the purchaseOrders to set
	 */
	public void setPurchaseOrders(List<PurchaseOrder> purchaseOrders) {
		this.purchaseOrders = purchaseOrders;
	}

	/**
	 * @return the regions
	 */
	public List<Region> getRegions() {
		return regions;
	}

	/**
	 * @param regions
	 *            the regions to set
	 */
	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the purchaseDetails
	 */
	public List<PurchaseDetails> getPurchaseDetails() {
		return purchaseDetails;
	}

	/**
	 * @param purchaseDetails
	 *            the purchaseDetails to set
	 */
	public void setPurchaseDetails(List<PurchaseDetails> purchaseDetails) {
		this.purchaseDetails = purchaseDetails;
	}

	/**
	 * @return the purchaseOrder
	 */
	public PurchaseOrder getPurchaseOrder() {
		return purchaseOrder;
	}

	/**
	 * @param purchaseOrder
	 *            the purchaseOrder to set
	 */
	public void setPurchaseOrder(PurchaseOrder purchaseOrder) {
		this.purchaseOrder = purchaseOrder;
	}

}
